# coding=utf-8
"""
Created on 2018年09月03日
"""
from setting_base.setting import C


class ArticleService(object):
    """
    """

    def __init__(self, processor):
        """
        """
        self._processor = processor
        self.download_url = C('storage')['download']['url']
        self.video_view_page_url = C('web')['viewPageUrl']

    def deal_one_article_return_data(
            self, userid,  article_info, comments_page_size, article_author_info=True, do_i_liked=True):
        """
        处理返回数据
        userid: 获取帖子列表的人(判断该用户是否点赞等逻辑)
        comments_page_size: 0-不需要查询帖子评论, else-每条帖子查询的评论数
        do_i_liked: True-需要查询我对这个帖子是否点赞, False-不需要
        article_author_info： True-需要查询帖子的作者信息, False-不需要
        """
        if not article_info:
            return dict()

        article_id = article_info['articleId']
        author_id = article_info['authorId']  # 帖子作者id

        article_authorid_list = list()
        if article_author_info:
            # 帖子作者
            article_authorid_list.append(author_id)

        # 从数据库获取帖子评论
        db_comments = list()
        comment_authorid_list = list()
        if comments_page_size:
            db_comments = self._processor.alive_cmsdb.page_get_comments_by_articleids(userid, [article_id], comments_page_size)
            if db_comments:
                comment_authorid_list = [e['authorId'] for e in db_comments]

        # 帖子和评论人员信息
        userid_list = list(set(article_authorid_list).union(set(comment_authorid_list)))
        users_info_dict = dict()
        if userid_list:
            users_info_dict = self.get_users_info(userid_list, userid)

        # 帖子的评论信息
        target_comments_dict = dict()
        if comments_page_size:
            target_comments_dict = self.deal_comments(db_comments, users_info_dict)

        # 判断我是否点赞
        liked_article_list = list()
        if do_i_liked:
            db_liked_list = self._processor.alive_cmsdb.batch_judge_article_like(userid, [article_id])
            liked_article_list = [e['articleId'] for e in db_liked_list]

        # 组装帖子信息
        hlsPath = article_info['hlsPath']
        target_article = dict(
            author=users_info_dict.get(author_id, dict()),
            articleId=article_id,
            viewPageUrl=self.video_view_page_url + article_info['encryptedArticleId'],
            regDate=str(article_info.get('regDate', '')),
            commentCount=article_info['commentCount'],
            likeCount=article_info['likeCount'],
            articleBody=article_info['articleBody'],
            articleState=article_info['contentState'],
            viewCount=article_info['cvCount'],
            shareCount=article_info['shareCount'],
            videoThumbnailPath=self.download_url + article_info['thumbPath_big'],
            sdVideoPath=self.download_url + article_info['videoPath_small'],
            hdVideoPath=self.download_url + article_info['videoPath_big'],
            hlsPath=self.download_url + hlsPath if hlsPath else '',
            length=article_info['length'],
            width=article_info['width'],
            height=article_info['height'],
            doILikeIt=1 if article_id in liked_article_list else 0,
            comments=target_comments_dict.get(article_id, list())
        )

        return target_article

    def batch_deal_articles_return_data(
            self, userid,  p_article_list, comments_page_size, article_author_info=True, do_i_liked=True):
        """
        处理返回数据
        userid: 获取帖子列表的人(判断该用户是否点赞等逻辑)
        comments_page_size: 0-不需要查询帖子评论, else-每条帖子查询的评论数
        do_i_liked: True-需要查询我对这个帖子是否点赞, False-不需要
        article_author_info： True-需要查询帖子的作者信息, False-不需要
        """
        if not p_article_list:
            return list()

        # 帖子id列表
        article_id_list = [e['articleId'] for e in p_article_list]
        article_id_list = list(set(article_id_list))  # 去重

        # 帖子作者id列表
        article_authorid_list = list()
        if article_author_info:
            article_authorid_list = [e['authorId'] for e in p_article_list]

        # 从数据库获取帖子评论
        db_comments = list()
        comment_authorid_list = list()
        if comments_page_size:
            db_comments = self._processor.alive_cmsdb.page_get_comments_by_articleids(userid, article_id_list, comments_page_size)
            if db_comments:
                comment_authorid_list = [e['authorId'] for e in db_comments]

        # 帖子和评论人员信息
        userid_list = list(set(article_authorid_list).union(set(comment_authorid_list)))
        users_info_dict = dict()
        if userid_list:
            users_info_dict = self.get_users_info(userid_list, userid)

        # 帖子的评论信息
        target_comments_dict = dict()
        if comments_page_size:
            target_comments_dict = self.deal_comments(db_comments, users_info_dict)

        # 判断我是否点赞
        liked_article_list = list()
        if do_i_liked:
            db_liked_list = self._processor.alive_cmsdb.batch_judge_article_like(userid, article_id_list)
            liked_article_list = [e['articleId'] for e in db_liked_list]

        # 整合帖子信息
        # select
        # a.articleId, a.articleBody, a.authorId, a.commentCount, a.likeCount, a.shareCount, a.cvCount,
        # a.videoPath_big, a.videoPath_small, a.length, a.width, a.height, a.thumbPath_big, a.thumbPath_small,
        # a.regDate, a.hlsPath, a.contentState, a.encryptedArticleId
        target_article_list = list()
        for each_article in p_article_list:
            articleId = each_article['articleId']  # 帖子id
            authorId = each_article['authorId']    # 帖子作者id
            hlsPath = each_article['hlsPath']
            this_article = dict(
                author=users_info_dict.get(authorId, dict()),
                articleId=articleId,
                viewPageUrl=self.video_view_page_url + each_article['encryptedArticleId'],
                regDate=str(each_article.get('regDate', '')),
                commentCount=each_article['commentCount'],
                likeCount=each_article['likeCount'],
                articleBody=each_article['articleBody'],
                articleState=each_article['contentState'],
                viewCount=each_article['cvCount'],
                shareCount=each_article['shareCount'],
                videoThumbnailPath=self.download_url + each_article['thumbPath_big'],
                sdVideoPath=self.download_url + each_article['videoPath_small'],
                hdVideoPath=self.download_url + each_article['videoPath_big'],
                hlsPath=self.download_url + hlsPath if hlsPath else '',
                length=each_article['length'],
                width=each_article['width'],
                height=each_article['height'],
                doILikeIt=1 if articleId in liked_article_list else 0,
                comments=target_comments_dict.get(articleId, list())
            )
            target_article_list.append(this_article)

        return target_article_list

    def deal_comments(self, db_comments, users_info_dict):
        """
        批量获取帖子的评论
        """
        comments_dict_data = dict()
        for each_comment in db_comments:
            article_id = each_comment['articleId']
            if article_id not in comments_dict_data.keys():
                comments_dict_data[article_id] = list()
            comments_dict_data[article_id].append(
                dict(
                    articleId=each_comment['articleId'],
                    commentId=each_comment['commentId'],
                    regDate=str(each_comment.get('regDate', '')),
                    commentBody=each_comment['commentBody'],
                    commenter=users_info_dict.get(each_comment['authorId'], dict())
                )
            )
        return comments_dict_data

    def get_users_info(self, target_userid_list, userid):
        """
        批量获取用户信息
        """
        # 去重
        target_userid_list = list(set(target_userid_list))
        db_users_info = self._processor.alive_cmsdb.batch_get_users_info(
            target_userid_list, check_doIFollow=True, check_isBlocked=True, userid=userid)

        # 返回数据
        users_info_dict_data = dict()
        for each_user_info in db_users_info:
            profileThumbPath_small = each_user_info['profileThumbPath_small']
            users_info_dict_data[each_user_info['userid']] = dict(
                userKey=each_user_info['userKey'],
                userName=each_user_info['userName'],
                userScore=each_user_info['userScore'],
                profilePicPath=self.download_url + profileThumbPath_small if profileThumbPath_small else '',
                doIFollow=each_user_info['doIFollow'],
                isBlocked=each_user_info['isBlocked']
            )

        return users_info_dict_data
